﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using iPark.Core.Storage;
using IPark.Metadata;
using System.Collections.ObjectModel;
using NokiaMapSDK;

namespace Shell.phone
{
    public partial class MainPage : PhoneApplicationPage
    {
        //double[] difx =
        //{		
        //0.70312500000000000000 	,	//0
        //0.35156250000000000000 	,	//1
        //0.17578125000000000000 	,	//2
        //0.17947009277971400000000000000 	,//3
        //0.09018358755677730000000000000 	,//4
        //0.04379593227461720000000000000 	,//5
        //0.02199196788480160000000000000 	,//6
        //0.01095409885339940000000000000 	,//7
        //0.00562807962474760000000000000 	,//8
        //0.00282386830320311000000000000 	,//9
        //0.00140258847593963000000000000 	,//10
        //0.00070522429592745000000000000 	,//11
        //0.00035175421086405600000000000 	,//12
        //0.00017564507758845000000000000 	,//13
        //0.00008792637207778530000000000 	,//14
        //0.00004407041332191440000000000 	,//15
        //0.00002195722591194740000000000 	,//16
        //0.00001097861295597370000000000 	,//17
        //0.00000550917412709330000000000 	,//18
        //0.00000274788490524119000000000 	,//19
        //0.00000137584842091850000000000 	,//20
        //};

        //double[] dify =
        //{		
        //0.70312500000000000000 	        ,   //0
        //0.35156250000000000000          ,	//1
        //0.17578125000000000000          ,	//2
        //0.21579246127700300000000000000 	,//3
        //0.10779862565195300000000000000 	,//4
        //0.05035084988412610000000000000 	,//5
        //0.02387211113177180000000000000 	,//6
        //0.01160921672098410000000000000 	,//7
        //0.00570327976044238000000000000 	,//8
        //0.00282763373335596000000000000 	,//9
        //0.00140672468362897000000000000 	,//10
        //0.00070389178800889900000000000 	,//11
        //0.00035243675752082600000000000 	,//12
        //0.00017558423899683800000000000 	,//13
        //0.00008777103696213910000000000 	,//14
        //0.00004383086686894240000000000 	,//15
        //0.00002194686234958270000000000 	,//16
        //0.00001096877328889950000000000 	,//17
        //0.00000548438664444972000000000 	,//18
        //0.00000274162945673336000000000 	,//19
        //0.00000137258825044077000000000 	,//20
        //};
        private GeoPoint center = new GeoPoint();
        private int iLevel = 10;

        private IStorage _storage = null;

        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);

            this._storage = new Storage();
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            nokiaMapControl1.Width = Application.Current.RootVisual.RenderSize.Width;
            nokiaMapControl1.Height = Application.Current.RootVisual.RenderSize.Height;

            center.Longitude = Convert.ToDouble("116.3977");
            center.Latitude = Convert.ToDouble("39.903");
            nokiaMapControl1.SetAppIDAndToken(DefaultSettings.GetAppID(), DefaultSettings.GetToken());
            nokiaMapControl1.SetZoomLevel(iLevel);
            nokiaMapControl1.SetCenter(center);
            nokiaMapControl1.Refresh();
        }

        //private void test_Click(object sender, RoutedEventArgs e)
        //{
        //    Storage storage = new Storage();

        //    List<StorageData> result = storage.QueryAllParkingData();
        //}

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            nokiaMapControl1.SetAppIDAndToken(DefaultSettings.GetAppID(), DefaultSettings.GetToken());
            nokiaMapControl1.SetCenter(center);
            nokiaMapControl1.Refresh();
        }

        private void GestureListener_PinchDelta(object sender, PinchGestureEventArgs e)
        {
            //（两点触摸）放大缩小地图事件处理

            //ZoomLevel(Max:20;Min:0)
            if (e.DistanceRatio<1)
            {
                iLevel = (int)(e.DistanceRatio * iLevel) < 0 ? 0 : (int)(e.DistanceRatio * iLevel);
            }
            else if(e.DistanceRatio>1)
            {
                iLevel = (int)(e.DistanceRatio / 5 * iLevel) > 20 ? 20 : (int)(e.DistanceRatio / 5 * iLevel);
            }
            this.nokiaMapControl1.SetZoomLevel(this.iLevel);
            this.nokiaMapControl1.Refresh();
        }

        //移动地图操作
        private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("H:" + e.HorizontalChange + ";V:" + e.VerticalChange);
            center.Longitude += e.HorizontalChange/(100.0*iLevel);
            center.Latitude += e.VerticalChange /(100.0 * iLevel);
            if (IsValidPos(center))
            {
                this.nokiaMapControl1.SetCenter(center);
                this.nokiaMapControl1.Refresh();
            }
        }

        //判断是否是有效的GPS点
        private bool IsValidPos(GeoPoint pos)
        {
            if (-180 > pos.Longitude
                || 180 < pos.Longitude
                || -90 > pos.Latitude
                || 90 < pos.Latitude)
            {
                return false;
            }

            return true;
        }

        
    }

    public partial class MainPage : PhoneApplicationPage
    {
        

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string searchText = this.txtSearch.Text;

            if (String.IsNullOrEmpty(searchText)) return;

            List<StorageData> results = this._storage.QueryNameLike(searchText);

            if (results == null) return;

            // 标注结果
            List<GeoPoint> resultPoints = new List<GeoPoint>();

            List<PlaceInfo> resultInfos = new List<PlaceInfo>();

            foreach (StorageData data in results)
            {
                if (data == null) continue;

                GeoPoint geoPoint = new GeoPoint();

                geoPoint.Longitude = Convert.ToDouble(data.LongitudeBaidu);
                geoPoint.Latitude = Convert.ToDouble(data.LatitudeBaidu);
                resultPoints.Add(geoPoint);

                //PlaceInfo placeInfo = new PlaceInfo();

                //placeInfo.CityName = data.Admin;
                //placeInfo.StreetName = data.FullName;

                //resultInfos.Add(placeInfo);
            }

            nokiaMapControl1.SetAppIDAndToken(DefaultSettings.GetAppID(), DefaultSettings.GetToken());
            nokiaMapControl1.MarkPOIs(resultPoints);

            //foreach (PlaceInfo info in resultInfos)
            //{
            //    nokiaMapControl1.SetPlaceInfo(info);
            //    nokiaMapControl1.SetInfoBoxPosition(InfoBoxPosition.RightMarker);
            //}

            nokiaMapControl1.Refresh();
        }
    }
}